<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改密码</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"
            integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ"
            crossorigin="anonymous"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>
</head>
<body background="/img/login.jpg" style="background-repeat: no-repeat;background-size: cover">
<div class="container">
    <div class="row" style="margin-top: 200px;">
        <div class="col-md-4 col-md-offset-4">
            <div style="margin:10px 10px 10px 10px">


                <div id="old_password_div" class="form-group input-group-lg">
                    <label style="font-size: 16px;" class="control-label">旧密码</label>
                    <input id="old_password" type="password" class="form-control" placeholder="不会忘记了吧">
                    <span id="message1" class="help-block" style="height: 12px;"></span>
                </div>

                <div id="new_password_div" class="form-group input-group-lg">
                    <label style="font-size: 16px;" class="control-label">新密码</label>
                    <input id="new_password" type="password" class="form-control" placeholder="想好了吗">
                    <span id="message2" class="help-block" style="height: 12px;"></span>
                </div>


                <div id="con_password_div" class="form-group input-group-lg">
                    <label style="font-size: 16px;" class="control-label">确认密码</label>
                    <input id="con_password" type="password" class="form-control" placeholder="确认是它吗">
                    <span id="message3" class="help-block" style="height: 12px;"></span>
                </div>

                <button id="sub_btn" type="submit" class="btn btn-default btn-lg btn-block">注册</button><br>
                <div class="row">
                    <div class="col-md-4">
                        <a href="index.jsp" class="button"><span
                                class="glyphicon glyphicon-grain glyphicon glyphicon-">返回主页</span></a>
                    </div>
                    <div class="col-md-4 col-md-offset-4">
                        <a href="forget.jsp" class="button"><span
                                class="glyphicon glyphicon-grain glyphicon glyphicon-">找回密码</span></a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    $(document).ready(function () {
        //恢复输入框状态
        $("#old_password").click(function () {
            $("#old_password_div").removeClass("has-error");
            $("#message1").text("");
        });
        $("#new_password").click(function () {
            $("#new_password_div").removeClass("has-error");
            $("#message2").text("");
        });
        $("#con_password").click(function () {
            $("#con_password_div").removeClass("has-error");
            $("#message3").text("");
        });

        $("#sub_btn").click(function () {
            var old_password = $("#old_password").val();
            var new_password = $("#new_password").val();
            var con_password = $("#con_password").val();
            if (old_password == null || old_password == "") {
                $("#old_password_div").addClass("has-error");
                $("#message1").text("密码不能为空");
                return;
            }
            if (new_password == null || new_password == "") {
                $("#new_password_div").addClass("has-error");
                $("#message2").text("密码不能为空");
                return;
            }
            if (con_password == null || con_password == "") {
                $("#con_password_div").addClass("has-error");
                $("#message3").text("密码不能为空");
                return;
            }
            if (new_password != con_password) {
                $("#new_password_div").addClass("has-error");
                $("#message2").text("两次密码输入不一致");
                return;
            }

            $.post("${pageContext.request.contextPath}/user/changePassword","oldPassword="+old_password+"&newPassword="+new_password,function (result) {
                alert(result);
                if (result=="logOff"){
                    location.replace("/login.jsp")
                }else if (result=="errorPassword"){
                    $("#old_password_div").addClass("has-error");
                    $("#message1").text("密码错误");
                    return;
                }else if (result=="notPassword"){
                    $("#new_password_div").addClass("has-error");
                    $("#message2").text("密码不合法");
                    return;
                }else if (result=="false"){
                    alert("更新失败");
                    return;
                }

                if (result=="true"){
                    alert("更新成功");
                }
            })
        });
    });
</script>
</html>
